package jude.java.money.service;

import java.util.Date;

import jude.java.money.beans.Log;
import jude.java.money.beans.def.LogActions;
import jude.java.util.Utils;


public class LogService {
	
	public static final int LOG_ERROR = 8;
	public static final int LOG_FAIL = 4;
	public static final int LOG_SUCCESS = 2;
	public static final int LOG_NORMAL = 1;
	
	private static BaseService service;
	private static int defaultLevel = LOG_NORMAL;
	private static int logFlag = 0;
	private static int controlLogFlag = Integer.MAX_VALUE;
	
	public static void insertLog(Log log) {
		if (log != null) {
			log.setTime(new Date());
			service.insert(log);
		}
	}
	
	private static void log(int flag, String target, String action) {
		if ((flag & logFlag) != 0) {
			Log l = new Log();
			l.setFlag(flag);
			l.setTarget(target);
			l.setAction(action);
			l.setTime(new Date());
			service.insert(l);
		}
		if ((flag & controlLogFlag) != 0) {
			StringBuilder sb = new StringBuilder();
			sb.append(target);		//.append(flag).append(',')
			if (action != null) {
				sb.append(',').append(action);
			}
			System.out.println(sb.toString());
		}
	}
	
	public static void l(Object... objs) {
		l(defaultLevel, objs);
	}
	
	public static void l(int flag, Object... objs) {
		if (objs == null || objs.length == 0)
			return;
		flag = flag <= 0 ? defaultLevel : flag;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < objs.length; i++) {
			if (i != 0)
				sb.append(',');
			sb.append(objs[i]);
		}
		if (sb.length() == 0)
			return;
		log(flag, LogActions.DEFAULT, sb.toString());
	}
	
	public static void e(String action) {
		error(null, action);
	}
	
	public static void e(Throwable e) {
		String action = Utils.toString(e);
		error(null, action);
		if ((LOG_ERROR & controlLogFlag) != 0) {
			e.printStackTrace();
		}
	}
	
	public static void error(String target, String action) {
		log(LOG_ERROR, target, action);
	}
	
	public static void fail(String target, String action) {
		log(LOG_FAIL, target, action);
	}
	
	public static void success(String target, String action) {
		log(LOG_SUCCESS, target, action);
	}
	
	public static void n(String target, String action) {
		log(LOG_NORMAL, target, action);
	}
	
	public static void error(String action) {
		error(null, action);
	}
	
	public static void fail(String action) {
		fail(null, action);
	}
	
	public static void success(String action) {
		success(null, action);
	}
	
	public static void n(String action) {
		n(null, action);
	}
	
	///////////////////////////////////////////////////////////////
	
	public void setGlobalService(GlobalService service) {
		LogService.service = service;
	}

	public static void setLogFlag(int logLevel) {
		LogService.logFlag = logLevel;
	}
	
	public static void setControlLogFlag(int controlLogFlag) {
		LogService.controlLogFlag = controlLogFlag;
	}
	
	public static void setDefaultLevel(int defaultLevel) {
		LogService.defaultLevel = defaultLevel;
	}
}
